/**
 * @description 收货地址创建和获取
 * @author 李哲
 */
const Address = require('../model/Address');

/**
 * 创建地址信息
 * @param {string} username 用户姓名
 * @param {object} data 地址信息
 * @returns 
 */
const setAddress = async (username, data) => {
    const createAddress = await Address.create({
        username,
        ...data
    })
    return createAddress
}
/**
 * 获取地址列表
 * @param {String} username 用户姓名
 * @returns 
 */
const getAddressList = async (username) => {
    const newAddress = await Address.find({ username }).sort({ updatedAt: -1 });
    return newAddress
}
/**
 * 获取单个地址
 * @param {String} id 地址id
 */
const getAddressOne = async (id) => {
    const newAddress = await Address.findById(id);
    return newAddress
}
/**
 * 修改地址信息
 * @param {string} id 地址id
 * @param {string} username 用户姓名
 * @param {Object} data 修改的数据
 */
const updateAddress = async (id, username, data) => {
    const newAddress = await Address.findByIdAndUpdate({
        _id: id,
        username
    }, {
        username,
        ...data
    }, {
        new: true
    })
    return newAddress
}
module.exports = { setAddress, getAddressList, getAddressOne, updateAddress };